package org.agilelang.object;

import org.agilelang.object.ObjectClass;
import org.agilelang.object.ObjectFunction;
import org.agilelang.object.ObjectParam;
import org.agilelang.object.ObjectProject;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Roderick
 */
public class Write {
// <editorfold defaultstate="collapsed" desc="Metodo app">

    ObjectProject agileproject = new ObjectProject();
    ObjectClass class_app = new ObjectClass();
    ObjectClass class_controller = new ObjectClass();
    ObjectClass class_model = new ObjectClass();
// </editorfold>

    void load_app() {
        class_app.setType("App");

        ObjectFunction define_app = new ObjectFunction();
        define_app.setName("define");

        ObjectParam name_param = new ObjectParam();
        name_param.setName("name");
        define_app.getParams().add(name_param);

        ObjectParam version_param = new ObjectParam();
        version_param.setName("version");
        define_app.getParams().add(version_param);

        ObjectParam author_param = new ObjectParam();
        author_param.setName("author");
        define_app.getParams().add(author_param);

        ObjectParam description_param = new ObjectParam();
        description_param.setName("description");
        define_app.getParams().add(description_param);

        class_app.getFunctions().add(define_app);
    }

    void load_controller() {
        class_controller.setType("Controller");


        /*ObjectFunction define_controller = new ObjectFunction();
         define_controller.setName("define");
         ObjectParam caption_param = new ObjectParam();
         caption_param.setName("caption");
         define_controller.getParams().add(caption_param);
         class_controller.getFunctions().add(define_controller);


         ObjectFunction addView_controller = new ObjectFunction();
         addView_controller.setName("addView");
         ObjectParam name_c_param = new ObjectParam();
         name_c_param.setName("name");
         addView_controller.getParams().add(name_c_param);
         ObjectParam title_param = new ObjectParam();
         title_param.setName("title");
         addView_controller.getParams().add(title_param);
         class_controller.getFunctions().add(addView_controller);*/
    }

    void load_model() {
        class_model.setType("Model");
    }

    void generate() {
        String path_xml = "E:\\agile\\AgileLang\\Test\\syntax.xml";
        JAXBContext jc = null;
        Marshaller marshaller;
        java.io.StringWriter sw = new StringWriter();
        try {
            jc = JAXBContext.newInstance(ObjectProject.class);
            marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(agileproject, sw);
        } catch (JAXBException ex) {
            System.out.println("Error --> " + ex.toString());
        }
        try {

            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path_xml)));
            System.out.println(path_xml);
            pw.print(sw.toString());
            pw.close();
        } catch (IOException e) {
        }
    }

    public void process() {
        load_app();
        load_controller();
        load_model();
        agileproject.setName("MVC");
        //agileproject.getClasses().add(class_app);
        agileproject.getClasses().add(class_controller);
        //agileproject.getClasses().add(class_model);
        generate();
    }

    public static void main(String[] args) {
        Write wr = new Write();
        wr.process();
    }
}
